﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3.Invoice
{
    using alldataset.fullldatasetTableAdapters;
     

    public partial class InvoicingForm : Form
    {
       // int i = 0;
        Transaction.InvoiceTransaction invtransaction = null;
        public InvoicingForm()
        {
            InitializeComponent();
            invtransaction = new Transaction.InvoiceTransaction();
            loadcustomercombobox();
            //get invoicecode automatically
            lblInvoiceno.Text = invtransaction.newinvoiceget();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }
      /// <summary>
      /// load the customer combobox
      /// </summary>
        public void loadcustomercombobox()
        {
            
            try
            {
            customermastertableTableAdapter cust = new customermastertableTableAdapter();
            cust.Connection = new OleDbConnection(Program.ConnStr);
            DataTable dt = cust.Getallcustomercode();
            if (dt.Rows.Count > 0)
            {
                cmbcutcode.ValueMember = "customerpk";
                cmbcutcode.DisplayMember = "companycode";

                txtcompanyname.Text = "companyname";
                    cmbcutcode.DataSource = dt;
            }
            }
            catch (Exception e)
            {

                MessageBox.Show("Cannot load Program at>>invoiceform. loadcustomercombobox()" + e);
            }
           
        }

        /// <summary>
        /// load completed jobcode against each customer
        /// </summary>
       public void loadcompljobcodecombobox()
        {
            if (cmbcutcode.Text ==null || cmbcutcode.Text == "")
            {

            }
            else
            {
                try
                {
                    completedcobcodeadapterTableAdapter cmpltjbcd = new completedcobcodeadapterTableAdapter();
                    cmpltjbcd.Connection = new OleDbConnection(Program.ConnStr);
                    DataTable dt = cmpltjbcd.GetData(int.Parse(cmbcutcode.SelectedValue.ToString()));
                    if (dt.Rows.Count > 0)
                    {
                        cmbjobcode.ValueMember = "jobpk";
                        cmbjobcode.DisplayMember = "jobcode";

                        txtcompanyname.Text = "companyname";
                        cmbjobcode.DataSource = dt;

                    }
                    else
                    {
                        MessageBox.Show("NO JobCode to be invoiced for " + cmbcutcode.Text);


                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show("Cannot load Program at>>invoiceform.loadcompljobcodecombobox()" + e);
                }

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       private void cmbcutcode_SelectedIndexChanged(object sender, EventArgs e)
       {
           loadcompljobcodecombobox();
           dataGridView1.DataSource = null;

       }

       private void cmbjobcode_SelectedValueChanged(object sender, EventArgs e)
       {
           
           tbltoinvoicedtableTableAdapter tbltoinvce = new tbltoinvoicedtableTableAdapter();
       tbltoinvce.Connection = new OleDbConnection(Program.ConnStr);
       if (cmbjobcode.SelectedValue != null)
       {
           
           tbltoinvce.Connection = new OleDbConnection(Program.ConnStr);

           if (cmbjobcode.SelectedValue != null)
           {

               DataTable dt1 = tbltoinvce.GetDataBy(int.Parse(cmbjobcode.SelectedValue.ToString()));
            
               DataTable dt = new DataTable();
               if (dataGridView1.DataSource != null)
               {
                    dt = (DataTable)dataGridView1.DataSource;
                   
                    foreach (DataRow drow in dt1.Rows)
                    {
                        //check for duplication if needed
                        //
                        DataRow[] selrows = dt.Select("JobCode='" + drow["JobCode"] + "'");
                        if(selrows.Length==0)
                        dt.ImportRow(drow);
                    }
               }
               else
               { dt = dt1; }
               dataGridView1.DataSource = dt;
               //dataGridView1.Columns[6].Visible = false;
               
           }
       }
   }

        /// <summary>
        /// get data from form and inser it to two tables
        /// the values of textboxes and comboboxes are added to tbl-invoicemastertable
        /// then the primarykey invoicepk is returned
        /// the data of gridview is then added to tblinvoicesubtable
        /// </summary>
       public void getdataforinvoice()
       {
           ArrayList arrylst = new ArrayList();
           arrylst.Add(lblInvoiceno.Text);
            arrylst.Add(DateTime.Now.Date);
           arrylst.Add(int.Parse(cmbcutcode.SelectedValue.ToString()));
           arrylst.Add(dtpduedate.Value.Date);
           arrylst.Add(txtsalesman.Text);
           arrylst.Add(cmbcurrency.Text);
               
           try
           {
               if (arrylst.Count != 0)
               {
                   if (invtransaction.insertintomasterinvoice(arrylst))
                   {
                       invoice_mastertableTableAdapter invmstrtbladapter = new invoice_mastertableTableAdapter();
                       invmstrtbladapter.Connection= new OleDbConnection(Program.ConnStr);
                       DataTable dt = invmstrtbladapter.Getnumbyinvoicenum(lblInvoiceno.Text,int.Parse(cmbcutcode.SelectedValue.ToString()));
                      
                       if (dt.Rows.Count>0)
                       {
                          // MessageBox.Show(dt.Rows[0][0].ToString());
                           for(int i=0;i<dataGridView1.Rows.Count-1;i++){
                               int invoicepk =int.Parse(dt.Rows[0][0].ToString());
                              
                               int jobpk=int.Parse(dataGridView1.Rows[i].Cells[7].Value.ToString());
                               ArrayList arrylst1 = new ArrayList();

                               if (invtransaction.insertintosubinvoice(jobpk, invoicepk))
                               {
                                   MessageBox.Show("Sucess");

                                   this.Close();

                               }

                              

                           }
                       }
                       }
                   }
               }
           
           catch(Exception)
           {
               MessageBox.Show(" Cannot Add Invoice");

               this.Close();
           }

       
       }


       private void btnSubmit_Click(object sender, EventArgs e)
       {
           getdataforinvoice();
       }

       private void InvoicingForm_Load(object sender, EventArgs e)
       {

       }


    /// <summary>
    /// clearcontrol()
    /// </summary>
       void clearControl()
       {
           txtsalesman.Text = "";
           txtsalesman.Text = "";
           cmbjobcode.DataSource = "";



       }

       
    }
}
